package gen.tools.host;

import gen.tools.Widgets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class EditHost extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public EditHost() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Widgets wid = new Widgets();
		request.setCharacterEncoding( wid.UIcodeSet );
		response.setContentType("application/text");
		response.setCharacterEncoding( wid.UIcodeSet );
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "";
		final String oper = request.getParameter("oper");									//增刪改查那一種
		final String subsysId = request.getParameter("subsysId");					//子系統ID
		final String hostIP = request.getParameter("hostIP");							//主機IP
		final int port = wid.getParNumberFromRequest( request.getParameter("port") );		//PORT
		String result = "";
		final String empId = wid.stringFilter( request.getParameter("empId") );
		final String loginId = wid.stringFilter( request.getParameter("loginId") );
		final String loginPWD = wid.stringFilter( request.getParameter("loginPWD") );
		
		try {
			conn = wid.getConn();
			/** 判別需進行何種操作 **/
			if ( "del".equalsIgnoreCase(oper) ) {
				sql = "DELETE FROM hosts WHERE ip=? AND subsysid=? AND port=?";
				ps = conn.prepareStatement(sql);
				ps.setString(1, hostIP );
				ps.setString(2, subsysId );
				ps.setInt(3, port);
				ps.execute();
				result = "刪除了"+hostIP;
			} else if ( "add".equalsIgnoreCase(oper) ) {
				if (! isDuplicated(subsysId,hostIP,port,conn,ps,rs) ) {
					sql = "INSERT INTO hosts (ip,subsysid,port,loginId,loginPWD,creator,createdDateTime) VALUES (?,?,?,?,?,?,?)";
					ps = conn.prepareStatement(sql);
					ps.setString(1, hostIP);
					ps.setString(2, subsysId);
					ps.setInt(3, port);
					ps.setString(4, loginId);
					ps.setString(5, loginPWD);
					ps.setString(6, empId);
					ps.setTimestamp(7, wid.cDT());
					ps.execute();
					result = "新增完成 "+hostIP;
				} else {
					result = hostIP+" 已存在 !";		
				}
			} else if ( "edit".equalsIgnoreCase(oper) ) {
				sql = "UPDATE hosts SET port=?,lastUpdator=?,lastUpdatedDateTime=?,loginId=?,loginPWD=? WHERE ip=? AND subsysid=? AND port=?";
				ps = conn.prepareStatement(sql);
				ps.setInt(1, port);
				ps.setString(2, empId);
				ps.setTimestamp(3, wid.cDT());
				ps.setString(4, loginId);
				ps.setString(5, loginPWD);
				ps.setString(6, hostIP);
				ps.setString(7, subsysId);
				ps.setInt(8, port);
				ps.execute();
				result = "修改完成!!";
			}
		} catch (Exception e) {
			result = "異動"+hostIP+"失敗!!\n\n";
			result += wid.errProcess(e);
			wid.errLog(e);
		} finally {
			try {
				if (rs!= null) {rs.close();}
				if (ps!= null) {ps.close();}
				if (conn!= null) {conn.close();}
			} catch (SQLException e) { System.out.println(wid.errProcess(e)); }			
		}
		
		response.getWriter().write(result);		
		
	}
	private boolean isDuplicated(String subSysId,String hostIP,int port,Connection conn,PreparedStatement ps, ResultSet rs) throws SQLException {
		String strSQL = "SELECT COUNT(*) FROM hosts WHERE ip=? AND subsysid=? AND port=?";
		
		ps = conn.prepareStatement(strSQL);
		ps.setString(1, hostIP);
		ps.setString(2, subSysId);
		ps.setInt(3, port);
		rs = ps.executeQuery();
		rs.next();

		return rs.getInt(1) == 0?false:true;
	}
}
